package com.ysd.lis.service.lab.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.PubEmployee;
import com.ysd.lis.entity.SysInterface;
import com.ysd.lis.entity.SysOrgDepartment;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacBloodCultrue;
import com.ysd.lis.entity.bac.BacObserResult;
import com.ysd.lis.entity.bac.BacStainingResult;
import com.ysd.lis.entity.bac.BacTestResult;
import com.ysd.lis.entity.cri.CriCriticalReport;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.bac.BacTestResultMapper;
import com.ysd.lis.mapper.lab.LabCriticalReportMapper;
import com.ysd.lis.mapper.lab.LabMaininfoMapper;
import com.ysd.lis.mapper.lab.LabRptresultMapper;
import com.ysd.lis.mapper.sys.SysInterfaceMapper;
import com.ysd.lis.mapper.sys.SysInterfaceMethExtParamMapper;
import com.ysd.lis.request.lab.*;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacBloodCultrueService;
import com.ysd.lis.service.bac.BacObserResultService;
import com.ysd.lis.service.bac.BacStainingResultService;
import com.ysd.lis.service.lab.LabCriticalReportService;
import com.ysd.util.RedisUserManager;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 危急值上报 服务实现类
 * </p>
 *
 * @author wanghao
 * @since 2024-02-20
 */
@Service
public class LabCriticalReportServiceImpl extends ServiceImpl<LabCriticalReportMapper, LabCriticalReport> implements LabCriticalReportService {

    @Autowired
    private LabCriticalReportMapper criticalReportMapper;
    @Autowired
    LabMaininfoMapper labMaininfoMapper;
    @Autowired
    LabRptresultMapper labRptresultMapper;
    @Autowired
    BacTestResultMapper bacTestResultMapper;
    @Autowired
    SysInterfaceMapper sysInterfaceMapper;
    @Autowired
    SysInterfaceMethExtParamMapper sysInterfaceMethExtParamMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Autowired
    BacBloodCultrueService bacBloodCultrueService;
    @Autowired
    BacObserResultService bacObserResultService;
    @Autowired
    BacStainingResultService bacStainingResultService;


    @Override
    public Result batchAddCriticalReportList(List<LabCriticalReport> labCriticalReports) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        String orgId = RedisUserManager.getStoreOrg("currentOrg" + token);

        SysUser sysUser = RedisUserManager.getUser();
        labCriticalReports.forEach(item -> {
            LambdaQueryWrapper<LabMaininfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabMaininfo::getRptGroup, item.getRptGroup())
                    .eq(LabMaininfo::getSampleda, item.getSampleda())
                    .eq(LabMaininfo::getSampleNo, item.getSampleNo())
                    .eq(LabMaininfo::getDelFlag, 0);
            final List<LabMaininfo> labMaininfos = labMaininfoMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(labMaininfos)) {
                item.setMaininfoId(labMaininfos.get(0).getId());
            }
            item.setOrgId(orgId);
        });
        boolean b = this.saveBatch(labCriticalReports);
        if (b) {
            List<String> ids = labCriticalReports.stream().map(LabCriticalReport::getId).collect(Collectors.toList());
            List<LabCriticalReportVo> criticalReportVos = new ArrayList<>();
            if ("2".equals(labCriticalReports.get(0).getCriticalType() + "")) {
                for (LabCriticalReport labCriticalReport : labCriticalReports) {
                    if ("3".equals(labCriticalReport.getCriticalItemType()) || "4".equals(labCriticalReport.getCriticalItemType())) {
                        MPJLambdaWrapper<LabCriticalReport> lambdaQueryWrapper = new MPJLambdaWrapper<>();
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getId, LabCriticalReportVo::getId);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportPerson, LabCriticalReportVo::getReportPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportTime, LabCriticalReportVo::getReportTime);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getNotifyPerson, LabCriticalReportVo::getNotifyPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getSampleNo, LabCriticalReportVo::getSampleNo);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReportVo::getMeasure);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getRequestNo, LabCriticalReportVo::getRequestNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getId, LabCriticalReportVo::getMainInfoId);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNo, LabCriticalReportVo::getPatNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNa, LabCriticalReportVo::getPatNa);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getReqDept, LabCriticalReportVo::getReqDept);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getOrgId, LabCriticalReportVo::getOrgId);
                        lambdaQueryWrapper.selectAs(BacTestResult::getGermNa, LabCriticalReportVo::getItemNa);
                        lambdaQueryWrapper.selectAs(BacTestResult::getGermNo, LabCriticalReportVo::getItemNo);
                        lambdaQueryWrapper.selectAs(BacTestResult::getTestResult, LabCriticalReportVo::getTestResult);
                        lambdaQueryWrapper.selectAs(LabReqMainQuery::getPatId, LabCriticalReportVo::getPatId);
                        lambdaQueryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
                        lambdaQueryWrapper.leftJoin(BacTestResult.class, on -> on.eq(BacTestResult::getId, LabCriticalReport::getResultId).eq(BacTestResult::getIsAlarm, 1));
                        lambdaQueryWrapper.leftJoin(LabReqMainQuery.class, LabReqMainQuery::getReqNo, LabMaininfo::getReqNo);
                        lambdaQueryWrapper.eq(LabCriticalReport::getId, labCriticalReport.getId());
                        lambdaQueryWrapper.eq(LabCriticalReport::getIsReport, 1);
                        lambdaQueryWrapper.eq(BacTestResult::getDelFlag, 0);
                        List<LabCriticalReportVo> labCriticalReportVos = criticalReportMapper.selectJoinList(LabCriticalReportVo.class, lambdaQueryWrapper);
                        if (CollectionUtils.isNotEmpty(labCriticalReportVos)) {
                            criticalReportVos.addAll(labCriticalReportVos);
                        }
                    } else if ("1".equals(labCriticalReport.getCriticalItemType())) {
                        MPJLambdaWrapper<LabCriticalReport> lambdaQueryWrapper = new MPJLambdaWrapper<>();
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getId, LabCriticalReportVo::getId);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportPerson, LabCriticalReportVo::getReportPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportTime, LabCriticalReportVo::getReportTime);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getNotifyPerson, LabCriticalReportVo::getNotifyPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getSampleNo, LabCriticalReportVo::getSampleNo);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReportVo::getMeasure);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getRequestNo, LabCriticalReportVo::getRequestNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getId, LabCriticalReportVo::getMainInfoId);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNo, LabCriticalReportVo::getPatNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNa, LabCriticalReportVo::getPatNa);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getReqDept, LabCriticalReportVo::getReqDept);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getOrgId, LabCriticalReportVo::getOrgId);
                        lambdaQueryWrapper.selectAs(BacBloodCultrue::getCulMediumNo, LabCriticalReportVo::getItemNa);
                        lambdaQueryWrapper.selectAs(BacBloodCultrue::getCulMediumNa, LabCriticalReportVo::getItemNo);
                        lambdaQueryWrapper.selectAs(BacBloodCultrue::getAlarmText, LabCriticalReportVo::getTestResult);
                        lambdaQueryWrapper.selectAs(LabReqMainQuery::getPatId, LabCriticalReportVo::getPatId);
                        lambdaQueryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
                        lambdaQueryWrapper.leftJoin(BacBloodCultrue.class, on -> on.eq(BacBloodCultrue::getId, LabCriticalReport::getResultId).eq(BacBloodCultrue::getIsAlarm, 1));
                        lambdaQueryWrapper.leftJoin(LabReqMainQuery.class, LabReqMainQuery::getReqNo, LabMaininfo::getReqNo);
                        lambdaQueryWrapper.eq(LabCriticalReport::getId, labCriticalReport.getId());
                        lambdaQueryWrapper.eq(LabCriticalReport::getIsReport, 1);
                        lambdaQueryWrapper.eq(BacBloodCultrue::getDelFlag, 0);
                        List<LabCriticalReportVo> labCriticalReportVos = criticalReportMapper.selectJoinList(LabCriticalReportVo.class, lambdaQueryWrapper);
                        if (CollectionUtils.isNotEmpty(labCriticalReportVos)) {
                            criticalReportVos.addAll(labCriticalReportVos);
                        }
                    } else if ("2".equals(labCriticalReport.getCriticalItemType())) {
                        MPJLambdaWrapper<LabCriticalReport> lambdaQueryWrapper = new MPJLambdaWrapper<>();
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getId, LabCriticalReportVo::getId);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportPerson, LabCriticalReportVo::getReportPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportTime, LabCriticalReportVo::getReportTime);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getNotifyPerson, LabCriticalReportVo::getNotifyPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getSampleNo, LabCriticalReportVo::getSampleNo);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReportVo::getMeasure);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getRequestNo, LabCriticalReportVo::getRequestNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getId, LabCriticalReportVo::getMainInfoId);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNo, LabCriticalReportVo::getPatNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNa, LabCriticalReportVo::getPatNa);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getReqDept, LabCriticalReportVo::getReqDept);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getOrgId, LabCriticalReportVo::getOrgId);
                        lambdaQueryWrapper.selectAs(BacObserResult::getMorphologyName, LabCriticalReportVo::getItemNa);
                        lambdaQueryWrapper.selectAs(BacObserResult::getIsolateNum, LabCriticalReportVo::getItemNo);
                        lambdaQueryWrapper.selectAs(BacObserResult::getAlarmText, LabCriticalReportVo::getTestResult);
                        lambdaQueryWrapper.selectAs(LabReqMainQuery::getPatId, LabCriticalReportVo::getPatId);
                        lambdaQueryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
                        lambdaQueryWrapper.leftJoin(BacObserResult.class, on -> on.eq(BacObserResult::getId, LabCriticalReport::getResultId).eq(BacObserResult::getIsAlarm, 1));
                        lambdaQueryWrapper.leftJoin(LabReqMainQuery.class, LabReqMainQuery::getReqNo, LabMaininfo::getReqNo);
                        lambdaQueryWrapper.eq(LabCriticalReport::getId, labCriticalReport.getId());
                        lambdaQueryWrapper.eq(LabCriticalReport::getIsReport, 1);
                        lambdaQueryWrapper.eq(BacObserResult::getDelFlag, 0);
                        List<LabCriticalReportVo> labCriticalReportVos = criticalReportMapper.selectJoinList(LabCriticalReportVo.class, lambdaQueryWrapper);
                        if (CollectionUtils.isNotEmpty(labCriticalReportVos)) {
                            criticalReportVos.addAll(labCriticalReportVos);
                        }
                    } else if ("5".equals(labCriticalReport.getCriticalItemType())) {
                        MPJLambdaWrapper<LabCriticalReport> lambdaQueryWrapper = new MPJLambdaWrapper<>();
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getId, LabCriticalReportVo::getId);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportPerson, LabCriticalReportVo::getReportPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getReportTime, LabCriticalReportVo::getReportTime);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getNotifyPerson, LabCriticalReportVo::getNotifyPerson);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getSampleNo, LabCriticalReportVo::getSampleNo);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReportVo::getMeasure);
                        lambdaQueryWrapper.selectAs(LabCriticalReport::getRequestNo, LabCriticalReportVo::getRequestNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getId, LabCriticalReportVo::getMainInfoId);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNo, LabCriticalReportVo::getPatNo);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getPatNa, LabCriticalReportVo::getPatNa);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getReqDept, LabCriticalReportVo::getReqDept);
                        lambdaQueryWrapper.selectAs(LabMaininfo::getOrgId, LabCriticalReportVo::getOrgId);
                        lambdaQueryWrapper.selectAs(BacStainingResult::getStainingConcNa, LabCriticalReportVo::getItemNa);
                        lambdaQueryWrapper.selectAs(BacStainingResult::getStaConclusionNo, LabCriticalReportVo::getItemNo);
                        lambdaQueryWrapper.selectAs(BacStainingResult::getDescText, LabCriticalReportVo::getTestResult);
                        lambdaQueryWrapper.selectAs(LabReqMainQuery::getPatId, LabCriticalReportVo::getPatId);
                        lambdaQueryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
                        lambdaQueryWrapper.leftJoin(BacStainingResult.class, on -> on.eq(BacStainingResult::getId, LabCriticalReport::getResultId).eq(BacStainingResult::getIsAlarm, 1));
                        lambdaQueryWrapper.leftJoin(LabReqMainQuery.class, LabReqMainQuery::getReqNo, LabMaininfo::getReqNo);
                        lambdaQueryWrapper.eq(LabCriticalReport::getId, labCriticalReport.getId());
                        lambdaQueryWrapper.eq(LabCriticalReport::getIsReport, 1);
                        lambdaQueryWrapper.eq(BacStainingResult::getDelFlag, 0);
                        List<LabCriticalReportVo> labCriticalReportVos = criticalReportMapper.selectJoinList(LabCriticalReportVo.class, lambdaQueryWrapper);
                        if (CollectionUtils.isNotEmpty(labCriticalReportVos)) {
                            criticalReportVos.addAll(labCriticalReportVos);
                        }
                    }
                }
            } else {
                MPJLambdaWrapper<LabCriticalReport> lambdaQueryWrapper = new MPJLambdaWrapper<>();
                lambdaQueryWrapper.distinct();
                lambdaQueryWrapper.selectAs(LabCriticalReport::getId, LabCriticalReportVo::getId);
                lambdaQueryWrapper.selectAs(LabCriticalReport::getReportPerson, LabCriticalReportVo::getReportPerson);
                lambdaQueryWrapper.selectAs(LabCriticalReport::getReportTime, LabCriticalReportVo::getReportTime);
                lambdaQueryWrapper.selectAs(LabCriticalReport::getNotifyPerson, LabCriticalReportVo::getNotifyPerson);
                lambdaQueryWrapper.selectAs(LabCriticalReport::getSampleNo, LabCriticalReportVo::getSampleNo);
                lambdaQueryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReportVo::getMeasure);
                lambdaQueryWrapper.selectAs(LabCriticalReport::getRequestNo, LabCriticalReportVo::getRequestNo);
                lambdaQueryWrapper.selectAs(LabMaininfo::getId, LabCriticalReportVo::getMainInfoId);
                lambdaQueryWrapper.selectAs(LabMaininfo::getPatNo, LabCriticalReportVo::getPatNo);
                lambdaQueryWrapper.selectAs(LabMaininfo::getPatNa, LabCriticalReportVo::getPatNa);
                lambdaQueryWrapper.selectAs(LabMaininfo::getReqDept, LabCriticalReportVo::getReqDept);
                lambdaQueryWrapper.selectAs(LabMaininfo::getOrgId, LabCriticalReportVo::getOrgId);
                lambdaQueryWrapper.selectAs(LabMaininfo::getBedNo, LabCriticalReportVo::getBedNo);

                lambdaQueryWrapper.selectAs(LabMaininfo::getAge, LabCriticalReportVo::getAge);
                lambdaQueryWrapper.selectAs(LabMaininfo::getSex, LabCriticalReportVo::getSex);
                lambdaQueryWrapper.selectAs(LabMaininfo::getReqDoctor, LabCriticalReportVo::getReqDoctor);
                lambdaQueryWrapper.selectAs(LabMaininfo::getAgeUnit, LabCriticalReportVo::getAgeUnit);
                lambdaQueryWrapper.selectAs(LabMaininfo::getPatType, LabCriticalReportVo::getPatType);

                lambdaQueryWrapper.selectAs(LabRptresult::getItemNa, LabCriticalReportVo::getItemNa);
                lambdaQueryWrapper.selectAs(LabRptresult::getItemNo, LabCriticalReportVo::getItemNo);
                lambdaQueryWrapper.selectAs(LabRptresult::getTestResult, LabCriticalReportVo::getTestResult);
                lambdaQueryWrapper.selectAs(LabRptresult::getRef, LabCriticalReportVo::getRef);
                lambdaQueryWrapper.selectAs(LabReqMainQuery::getPatId, LabCriticalReportVo::getPatId);
                lambdaQueryWrapper.leftJoin(LabMaininfo.class, on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
                lambdaQueryWrapper.leftJoin(LabRptresult.class, on -> on.eq(LabRptresult::getId, LabCriticalReport::getResultId).eq(LabRptresult::getIsAlarm, 1));
                lambdaQueryWrapper.leftJoin(LabReqMainQuery.class, LabReqMainQuery::getReqNo, LabMaininfo::getReqNo);
                lambdaQueryWrapper.in(LabCriticalReport::getId, ids);
                lambdaQueryWrapper.eq(LabCriticalReport::getIsReport, 1);
                criticalReportVos = criticalReportMapper.selectJoinList(LabCriticalReportVo.class, lambdaQueryWrapper);
            }
            //获取数据字典(病人类型、性别、证件类型)
            List<Map<String, Object>> measureList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("measure").getData();

            for (LabCriticalReportVo labCriticalReportVo : criticalReportVos) {

                LambdaQueryWrapper<LabCriticalReport> reusltWrapper = new LambdaQueryWrapper<>();

                reusltWrapper.eq(LabCriticalReport::getItemNo, labCriticalReportVo.getItemNo());
                reusltWrapper.eq(LabCriticalReport::getPatNo, labCriticalReportVo.getPatNo());
                List<LabCriticalReport> labCriticalReports1 = criticalReportMapper.selectList(reusltWrapper);
                labCriticalReportVo.setNumber(String.valueOf(labCriticalReports1.size()));
                if (StringUtils.isNotBlank(labCriticalReportVo.getMeasure()) && CollectionUtils.isNotEmpty(measureList)) {
                    measureList.stream().filter(a -> a.get("code").equals(labCriticalReportVo.getMeasure())).findFirst().ifPresent(stringObjectMap -> labCriticalReportVo.setMeasure((String) stringObjectMap.get("name")));
                }
            }

            LambdaQueryWrapper<SysInterface> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysInterface::getDelFlag, 0);
            wrapper.eq(SysInterface::getIsEnable, 1);
            List<SysInterface> sysInterfaces = sysInterfaceMapper.selectList(wrapper);
            List<String> collect11 = sysInterfaces.stream().filter(o -> Objects.nonNull(o) && StringUtils.isNotBlank(o.getInterfaceUrl())).map(SysInterface::getInterfaceUrl).distinct().collect(Collectors.toList());
            try {
                if (CollectionUtils.isNotEmpty(collect11)) {
                    for (String webUrl : collect11) {

                        //上报危急值平台
                        List<CriCriticalReport> criList = new ArrayList<>();
                        String reportUrl111 = webUrl + "/CRI/addCriticalReport";
                        for (LabCriticalReportVo criticalReportVo : criticalReportVos) {
                            CriCriticalReport criCriticalReport = new CriCriticalReport();
                            criCriticalReport.setCriticalNo(criticalReportVo.getId());
                            criCriticalReport.setPatNo(criticalReportVo.getPatNo());
                            criCriticalReport.setPatNa(criticalReportVo.getPatNa());
                            criCriticalReport.setItemNo(criticalReportVo.getItemNo());
                            criCriticalReport.setItemNa(criticalReportVo.getItemNa());
                            criCriticalReport.setSystemNo("LIS");
                            criCriticalReport.setReportVal(criticalReportVo.getTestResult());
                            criCriticalReport.setReportPersonNo(sysUser.getUsername());
                            criCriticalReport.setReportPersonNa(sysUser.getRealname());
                            criCriticalReport.setReportTime(Timestamp.valueOf(criticalReportVo.getReportTime()));
                            criCriticalReport.setCurrentResult(criticalReportVo.getTestResult());
                            criCriticalReport.setDeptNo(criticalReportVo.getReqDept());
                            criCriticalReport.setReportDeptNo(criticalReportVo.getReqDept());
                            criCriticalReport.setDoctorPerson(criticalReportVo.getNotifyPerson());
                            criCriticalReport.setBedNo(criticalReportVo.getBedNo());
                            criCriticalReport.setAge(criticalReportVo.getAge());
                            criCriticalReport.setAgeUnit(criticalReportVo.getAgeUnit());
                            criCriticalReport.setPatType(criticalReportVo.getPatType());
                            criCriticalReport.setReqDoctor(criticalReportVo.getReqDoctor());
                            criCriticalReport.setSex(criticalReportVo.getSex());
                            criCriticalReport.setStateCode(0);
                            if ("2".equals(labCriticalReports.get(0).getCriticalType() + "")) {
                                criCriticalReport.setCriticalType(2D);
                            } else {
                                criCriticalReport.setCriticalType(1D);
                            }
                            criCriticalReport.setReportDeptNa(criticalReportVo.getReqDept());
                            criCriticalReport.setReportDeptNo(criticalReportVo.getReqDept());
                            criList.add(criCriticalReport);
                        }
                        HttpUtil.createPost(reportUrl111).body(JSONUtil.toJsonStr(criList)).contentType("application/json").execute().body();


                        if (webUrl.indexOf("MQ") > -1 || webUrl.indexOf("HL7") > -1 || webUrl.indexOf("tianjian") > -1) {
                            String reportUrl = webUrl + "/pushWjz/pushWjzInterface";
                            Map map = new HashMap();
                            map.put("labCriticalReportVos", criticalReportVos);
                            map.put("username", sysUser.getUsername());
                            map.put("realname", sysUser.getRealname());
                            String body = HttpUtil.createPost(reportUrl).body(JSONUtil.toJsonStr(map)).contentType("application/json").execute().body();
                            JSONObject reportObject = new JSONObject(body);
                            JSONObject reportStatus = reportObject.getJSONObject("status");
                            String reportCode = reportStatus.get("code").toString();
                            if ("400".equals(reportCode)) {
                                List<String> collect = criticalReportVos.stream().map(LabCriticalReportVo::getId).collect(Collectors.toList());
                                this.removeByIds(collect);
                                return Result.fail(reportStatus.get("msg").toString());
                            }
                        } else {
                            for (LabCriticalReportVo labCriticalReport : criticalReportVos) {
                                if (StringUtils.isNotBlank(labCriticalReport.getReportTime())) {
                                    labCriticalReport.setReportTime(labCriticalReport.getReportTime().substring(0, 19));
                                }
                                if (StringUtils.isNotBlank(labCriticalReport.getCreateTime())) {
                                    labCriticalReport.setCreateTime(labCriticalReport.getCreateTime().substring(0, 19));
                                }
                                String reportUrl = webUrl + "/pushWjz/pushWjzInterface";
                                String body = HttpUtil.createPost(reportUrl).body(JSONUtil.toJsonStr(labCriticalReport)).contentType("application/json").execute().body();
                                try {
                                    JSONObject reportObject = new JSONObject(body);
                                    JSONObject reportStatus = reportObject.getJSONObject("status");
                                    String reportCode = reportStatus.get("code").toString();
                                    if (!"400".equals(reportCode)) {
                                        return Result.succ(1, "上报成功", null);
                                    }
                                } catch (Exception e) {

                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException("接口管理平台异常,请检查");
            }
        }
        return Result.succ(1, "更新成功", null);
    }

    @Override
    public Result hisUpdateCritical(LabCriticalReport labCriticalReport) {

        if (Objects.isNull(labCriticalReport.getIsReviewed())) {
            labCriticalReport.setIsReviewed(1);
        }
        boolean b = this.updateById(labCriticalReport);
        if (b) {
            return Result.succ(1, "更新成功", null);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result deleteCriticalReportInfo(LabCriticalReport labCriticalReport) {
        if (StringUtils.isBlank(labCriticalReport.getId())) {
            return Result.fail("请选择要删除的记录");
        }
        if (this.removeById(labCriticalReport.getId())) {
            return Result.succ(1, "删除成功", null);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result findCriticalReportInfo(CriticalReportDto criticalReportDto) {
        MPJLambdaWrapper<LabCriticalReport> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(LabCriticalReport::getReportTime, CriticalReportVo::getReportTime);
        queryWrapper.selectAs("lrg", LabRptGroup::getRptGroupName, CriticalReportVo::getRptGroupName);
        queryWrapper.selectAs("lm", LabMaininfo::getSampleda, CriticalReportVo::getSampleda);
        queryWrapper.selectAs("lm", LabMaininfo::getSampleNo, CriticalReportVo::getSampleNo);
        queryWrapper.selectAs("lm", LabMaininfo::getReqNo, CriticalReportVo::getReqNo);
        queryWrapper.selectAs("lm", LabMaininfo::getPatNo, CriticalReportVo::getPatNo);
        queryWrapper.selectAs("lm", LabMaininfo::getPatNa, CriticalReportVo::getPatNa);
        queryWrapper.selectAs("lm", LabMaininfo::getTraits, CriticalReportVo::getTraits);
        queryWrapper.selectAs("lm", LabMaininfo::getReqTime, CriticalReportVo::getReqTime);
        queryWrapper.selectAs("lm", LabMaininfo::getTestTime, CriticalReportVo::getTestTime);
        queryWrapper.selectAs("lm", LabMaininfo::getChkTime, CriticalReportVo::getChkTime);
        queryWrapper.selectAs("sod", SysOrgDepartment::getName, CriticalReportVo::getReqDept);
        queryWrapper.selectAs("lm", LabMaininfo::getBedNo, CriticalReportVo::getBedNo);
        queryWrapper.selectAs("lr", LabRptresult::getItemNa, CriticalReportVo::getItemNa);
        queryWrapper.selectAs("lr", LabRptresult::getTestResult, CriticalReportVo::getTestResult);
        queryWrapper.selectAs("lr", LabRptresult::getRef, CriticalReportVo::getRef);
        queryWrapper.selectAs("sa", LabSampleType::getSampleTypeName, CriticalReportVo::getSampleTypeName);
        queryWrapper.selectAs("req", PubEmployee::getName, CriticalReportVo::getNotifyPerson);
        queryWrapper.selectAs("approval", SysUser::getRealname, CriticalReportVo::getReportPerson);
        queryWrapper.selectAs(LabCriticalReport::getRequestNo, CriticalReportVo::getRequestNo);
        queryWrapper.selectAs(LabCriticalReport::getRemark, CriticalReportVo::getRemark);
        queryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReport::getMeasure);
        queryWrapper.selectAs(LabCriticalReport::getCreateTime, CriticalReportVo::getCreateTime);
        queryWrapper.selectAs(LabCriticalReport::getId, CriticalReportVo::getId);
        queryWrapper.selectAs(LabCriticalReport::getReviewerTime, CriticalReportVo::getReviewerTime);
        queryWrapper.selectAs(LabCriticalReport::getTreatmentMeasure, CriticalReportVo::getTreatmentMeasure);
        queryWrapper.leftJoin(LabRptresult.class, "lr", LabRptresult::getId, LabCriticalReport::getResultId);
        queryWrapper.leftJoin(LabMaininfo.class, "lm", on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
        queryWrapper.leftJoin(LabRptGroup.class, "lrg", LabRptGroup::getRptGroup, LabMaininfo::getRptGroup);
        queryWrapper.leftJoin(SysOrgDepartment.class, "sod", SysOrgDepartment::getCode, LabMaininfo::getReqDept);
        queryWrapper.leftJoin(LabSampleType.class, "sa", LabSampleType::getSampleTypeCode, LabMaininfo::getSampleType);
        queryWrapper.leftJoin(PubEmployee.class, "req", p -> p.eq(PubEmployee::getCode, LabCriticalReport::getNotifyPerson).eq(PubEmployee::getDelFlag, 0));
        queryWrapper.leftJoin(SysUser.class, "approval", SysUser::getUsername, LabCriticalReport::getReportPerson);

        queryWrapper.ne(LabRptGroup::getBusinessType, "20");
        queryWrapper.eq(LabCriticalReport::getDelFlag, 0);
        queryWrapper.eq(LabRptresult::getDelFlag, 0);
        queryWrapper.eq(LabMaininfo::getDelFlag, 0);

        if (StringUtils.isNotEmpty(criticalReportDto.getScheduleDateStart()) && StringUtils.isNotEmpty(criticalReportDto.getScheduleDateEnd())) {
            Timestamp startTime = Timestamp.valueOf(criticalReportDto.getScheduleDateStart());
            Timestamp endTime = Timestamp.valueOf(criticalReportDto.getScheduleDateEnd());
            queryWrapper.ge(LabMaininfo::getSampleda, startTime);
            queryWrapper.le(LabMaininfo::getSampleda, endTime);
        }
        if (StringUtils.isNotEmpty(criticalReportDto.getNoticeDateStart()) && StringUtils.isNotEmpty(criticalReportDto.getNoticeDateEnd())) {
            Timestamp startTime = Timestamp.valueOf(criticalReportDto.getNoticeDateStart());
            Timestamp endTime = Timestamp.valueOf(criticalReportDto.getNoticeDateEnd());
            queryWrapper.ge(LabCriticalReport::getReportTime, startTime);
            queryWrapper.le(LabCriticalReport::getReportTime, endTime);
        }
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getOrgId()), LabMaininfo::getOrgId, criticalReportDto.getOrgId());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getRptGroup()), LabMaininfo::getRptGroup, criticalReportDto.getRptGroup());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getItemNo()), LabRptresult::getItemNo, criticalReportDto.getItemNo());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getPatType()), LabMaininfo::getPatType, criticalReportDto.getPatType());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getReqDept()), LabMaininfo::getReqDept, criticalReportDto.getReqDept());

        queryWrapper.like(StringUtils.isNotBlank(criticalReportDto.getPatNo()), LabMaininfo::getPatNo, criticalReportDto.getPatNo());
        queryWrapper.like(StringUtils.isNotBlank(criticalReportDto.getPatNa()), LabMaininfo::getPatNa, criticalReportDto.getPatNa());

        List<CriticalReportVo> criticalReportVos = criticalReportMapper.selectJoinList(CriticalReportVo.class, queryWrapper);
        List<Map<String, Object>> measure = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("measure").getData();
        for (CriticalReportVo criticalReportVo : criticalReportVos) {
            if (StringUtils.isNotBlank(criticalReportVo.getMeasure()) && CollectionUtils.isNotEmpty(measure)) {
                measure.stream().filter(a -> a.get("code").equals(criticalReportVo.getMeasure())).findFirst().ifPresent(stringObjectMap -> criticalReportVo.setMeasure((String) stringObjectMap.get("name")));
            }
        }


        return Result.succ(1, "查询成功", criticalReportVos);
    }

    @Override
    public Result findBacCriticalReportInfo(CriticalReportDto criticalReportDto) {
        MPJLambdaWrapper<LabCriticalReport> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(LabCriticalReport::getReportTime, CriticalReportVo::getReportTime);
        queryWrapper.selectAs("lrg", LabRptGroup::getRptGroupName, CriticalReportVo::getRptGroupName);
        queryWrapper.selectAs("lm", LabMaininfo::getSampleda, CriticalReportVo::getSampleda);
        queryWrapper.selectAs("lm", LabMaininfo::getSampleNo, CriticalReportVo::getSampleNo);
        queryWrapper.selectAs("lm", LabMaininfo::getReqNo, CriticalReportVo::getReqNo);
        queryWrapper.selectAs("lm", LabMaininfo::getPatNo, CriticalReportVo::getPatNo);
        queryWrapper.selectAs("lm", LabMaininfo::getPatNa, CriticalReportVo::getPatNa);
        queryWrapper.selectAs("lm", LabMaininfo::getTraits, CriticalReportVo::getTraits);
        queryWrapper.selectAs("lm", LabMaininfo::getReqTime, CriticalReportVo::getReqTime);
        queryWrapper.selectAs("lm", LabMaininfo::getTestTime, CriticalReportVo::getTestTime);
        queryWrapper.selectAs("lm", LabMaininfo::getChkTime, CriticalReportVo::getChkTime);
        queryWrapper.selectAs("sod", SysOrgDepartment::getName, CriticalReportVo::getReqDept);
        queryWrapper.selectAs("lm", LabMaininfo::getBedNo, CriticalReportVo::getBedNo);
        queryWrapper.selectAs("sa", LabSampleType::getSampleTypeName, CriticalReportVo::getSampleTypeName);
        queryWrapper.selectAs("req", PubEmployee::getName, CriticalReportVo::getNotifyPerson);
        queryWrapper.selectAs("approval", SysUser::getRealname, CriticalReportVo::getReportPerson);
        queryWrapper.selectAs(LabCriticalReport::getRequestNo, CriticalReportVo::getRequestNo);
        queryWrapper.selectAs(LabCriticalReport::getRemark, CriticalReportVo::getRemark);
        queryWrapper.selectAs(LabCriticalReport::getMeasure, LabCriticalReport::getMeasure);
        queryWrapper.selectAs(LabCriticalReport::getCreateTime, CriticalReportVo::getCreateTime);
        queryWrapper.selectAs(LabCriticalReport::getId, CriticalReportVo::getId);
        queryWrapper.selectAs(LabCriticalReport::getReviewerTime, CriticalReportVo::getReviewerTime);
        queryWrapper.selectAs(LabCriticalReport::getTreatmentMeasure, CriticalReportVo::getTreatmentMeasure);
        queryWrapper.selectAs(LabCriticalReport::getCriticalItemType, CriticalReportVo::getCriticalItemType);
        queryWrapper.selectAs(LabCriticalReport::getCurrentResult, CriticalReportVo::getCurrentResult);
        queryWrapper.leftJoin(LabMaininfo.class, "lm", on -> on.eq(LabMaininfo::getRptGroup, LabCriticalReport::getRptGroup).eq(LabMaininfo::getSampleNo, LabCriticalReport::getSampleNo).eq(LabMaininfo::getSampleda, LabCriticalReport::getSampleda));
        queryWrapper.leftJoin(LabRptGroup.class, "lrg", LabRptGroup::getRptGroup, LabMaininfo::getRptGroup);
        queryWrapper.leftJoin(SysOrgDepartment.class, "sod", SysOrgDepartment::getCode, LabMaininfo::getReqDept);
        queryWrapper.leftJoin(LabSampleType.class, "sa", LabSampleType::getSampleTypeCode, LabMaininfo::getSampleType);
        queryWrapper.leftJoin(PubEmployee.class, "req", p -> p.eq(PubEmployee::getCode, LabCriticalReport::getNotifyPerson).eq(PubEmployee::getDelFlag, 0));
        queryWrapper.leftJoin(SysUser.class, "approval", SysUser::getUsername, LabCriticalReport::getReportPerson);

        queryWrapper.eq(LabRptGroup::getBusinessType, "20");
        queryWrapper.eq(LabCriticalReport::getDelFlag, 0);
        queryWrapper.eq(LabMaininfo::getDelFlag, 0);

        if (StringUtils.isNotEmpty(criticalReportDto.getScheduleDateStart()) && StringUtils.isNotEmpty(criticalReportDto.getScheduleDateEnd())) {
            Timestamp startTime = Timestamp.valueOf(criticalReportDto.getScheduleDateStart());
            Timestamp endTime = Timestamp.valueOf(criticalReportDto.getScheduleDateEnd());
            queryWrapper.ge(LabMaininfo::getSampleda, startTime);
            queryWrapper.le(LabMaininfo::getSampleda, endTime);
        }
        if (StringUtils.isNotEmpty(criticalReportDto.getNoticeDateStart()) && StringUtils.isNotEmpty(criticalReportDto.getNoticeDateEnd())) {
            Timestamp startTime = Timestamp.valueOf(criticalReportDto.getNoticeDateStart());
            Timestamp endTime = Timestamp.valueOf(criticalReportDto.getNoticeDateEnd());
            queryWrapper.ge(LabCriticalReport::getReportTime, startTime);
            queryWrapper.le(LabCriticalReport::getReportTime, endTime);
        }
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getOrgId()), LabMaininfo::getOrgId, criticalReportDto.getOrgId());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getRptGroup()), LabMaininfo::getRptGroup, criticalReportDto.getRptGroup());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getPatType()), LabMaininfo::getPatType, criticalReportDto.getPatType());
        queryWrapper.eq(StringUtils.isNotBlank(criticalReportDto.getReqDept()), LabMaininfo::getReqDept, criticalReportDto.getReqDept());

        queryWrapper.like(StringUtils.isNotBlank(criticalReportDto.getPatNo()), LabMaininfo::getPatNo, criticalReportDto.getPatNo());
        queryWrapper.like(StringUtils.isNotBlank(criticalReportDto.getPatNa()), LabMaininfo::getPatNa, criticalReportDto.getPatNa());

        List<CriticalReportVo> criticalReportVos = criticalReportMapper.selectJoinList(CriticalReportVo.class, queryWrapper);
        List<Map<String, Object>> measure = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("measure").getData();
        for (CriticalReportVo criticalReportVo : criticalReportVos) {
            if (StringUtils.isNotBlank(criticalReportVo.getMeasure()) && CollectionUtils.isNotEmpty(measure)) {
                measure.stream().filter(a -> a.get("code").equals(criticalReportVo.getMeasure())).findFirst().ifPresent(stringObjectMap -> criticalReportVo.setMeasure((String) stringObjectMap.get("name")));
            }
        }


        return Result.succ(1, "查询成功", criticalReportVos);
    }

    //危急值提醒程序调用
    @Override
    public List<AlarmReportVo> findAlarmReportInfo(AlarmReportDto alarmReportDto) {

        LambdaQueryWrapper<LabCriticalReport> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(LabCriticalReport::getDelFlag, 0);
        wrapper.eq(LabCriticalReport::getIsReviewed, alarmReportDto.getIsReviewed());

        Timestamp startTime = Timestamp.valueOf(alarmReportDto.getBeginDate().substring(0, 10) + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(alarmReportDto.getEndDate().substring(0, 10) + " 23:59:59");

        wrapper.ge(LabCriticalReport::getReportTime, startTime).le(LabCriticalReport::getReportTime, endTime);

        List<LabCriticalReport> labCriticalReports = criticalReportMapper.selectList(wrapper);

        List<AlarmReportVo> alarmReportVoList = new ArrayList<>();

        for (LabCriticalReport criticalReport : labCriticalReports) {
            AlarmReportVo alarmReportVo = new AlarmReportVo();

            Timestamp startTime1 = Timestamp.valueOf(criticalReport.getSampleda().toString().substring(0, 10) + " 00:00:00");
            Timestamp endTime1 = Timestamp.valueOf(criticalReport.getSampleda().toString().substring(0, 10) + " 23:59:59");

            LambdaQueryWrapper<LabMaininfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabMaininfo::getDelFlag, 0);

            // 添加一组条件，当RptGroup, SampleNo, Sampleda 都满足时
            lambdaQueryWrapper.and(w -> w.eq(LabMaininfo::getRptGroup, criticalReport.getRptGroup()).eq(LabMaininfo::getSampleNo, criticalReport.getSampleNo()).ge(LabMaininfo::getSampleda, startTime1).le(LabMaininfo::getSampleda, endTime1));

            List<LabMaininfo> labMaininfos = labMaininfoMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtils.isEmpty(labMaininfos)) {
                return null;
            }
            LabMaininfo labMaininfo = labMaininfos.get(0);
            //判断病人类型
            if (alarmReportDto.getPatType().size() > 0) {
                boolean b = alarmReportDto.getPatType().contains(labMaininfo.getPatType());
                if (!b) {
                    labCriticalReports.remove(criticalReport);
                    continue;
                }
            }
            //判断科室
            if (alarmReportDto.getReqDept().size() > 0) {
                boolean b = alarmReportDto.getReqDept().contains(labMaininfo.getReqDept());
                if (!b) {
                    labCriticalReports.remove(criticalReport);
                    continue;
                }
            }
            //判断报告单元
            if (alarmReportDto.getRptGroup().size() > 0) {
                if (!Objects.equals(alarmReportDto.getRptGroup(), labMaininfo.getRptGroup())) {
                    labCriticalReports.remove(criticalReport);
                    continue;
                }
            }

            alarmReportVo.setAlarmId(criticalReport.getId());
            alarmReportVo.setNoticeDate(criticalReport.getReportTime());
            alarmReportVo.setIsReceived(criticalReport.getIsReviewed());
            alarmReportVo.setReqDept(labMaininfo.getReqDept());
            alarmReportVo.setReqDoctor(labMaininfo.getReqDoctorNa());
            alarmReportVo.setReqNo(labMaininfo.getReqNo());
            alarmReportVo.setPatNo(labMaininfo.getPatNo());
            alarmReportVo.setPatNa(labMaininfo.getPatNa());
            alarmReportVo.setSex(labMaininfo.getSex());
            alarmReportVo.setTreatmentMeasure(criticalReport.getTreatmentMeasure());
            alarmReportVo.setBedNo(labMaininfo.getBedNo());
            alarmReportVo.setTestPurpose(labMaininfo.getTestPurpose());
            alarmReportVo.setSampleDa(labMaininfo.getSampleda());
            alarmReportVo.setReportGroup(labMaininfo.getRptGroup());
            alarmReportVo.setSampleNo(labMaininfo.getSampleNo());
            alarmReportVo.setTestDoctor(labMaininfo.getTestDoctorNa());
            alarmReportVo.setChkDoctor(labMaininfo.getChkDoctorNa());
            alarmReportVo.setChkTime(labMaininfo.getChkTime());
            alarmReportVo.setMainInfoId(labMaininfo.getId());

            String resultId = criticalReport.getResultId();
            LabRptresult labRptresult = labRptresultMapper.selectById(resultId);
            if (labRptresult == null) {
                return null;
            }

            alarmReportVo.setItemNo(labRptresult.getItemNo());
            alarmReportVo.setItemNa(labRptresult.getItemNa());
            alarmReportVo.setResultText(labRptresult.getTestResult());
            alarmReportVo.setResultRef(labRptresult.getRef());
            alarmReportVo.setReviewerDoctor(criticalReport.getReviewerDoctor());
            alarmReportVo.setReviewerTime(criticalReport.getReviewerTime());
            alarmReportVoList.add(alarmReportVo);
        }

        return alarmReportVoList;
    }

    //审核危急值
    @Override
    public String handleAlarm(AlarmChkDto dto) {
        LabCriticalReport criticalReport = new LabCriticalReport();
        criticalReport.setId(dto.getAlarmId());
        criticalReport.setTreatmentMeasure(dto.getTreatmentMeasure());
        criticalReport.setReviewerDoctor(dto.getReviewerDoctor());
        Timestamp time = Timestamp.valueOf(dto.getReviewerTime());
        criticalReport.setReviewerTime(time);
        criticalReport.setIsReviewed(1);
        int i = criticalReportMapper.updateById(criticalReport);
        if (i > 0) {
            return "修改成功";
        } else {
            return null;
        }

    }

    //获取结果信息
    @Override
    public List<AlarmRptresultVo> getResultInfo(String id) {
        LambdaQueryWrapper<LabRptresult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabRptresult::getDelFlag, 0);
        queryWrapper.eq(LabRptresult::getMaininfoId, id);
        List<LabRptresult> labRptresults = labRptresultMapper.selectList(queryWrapper);

        List<AlarmRptresultVo> alarmRptresultVoList = new ArrayList<>();
        for (LabRptresult labRptresult : labRptresults) {
            AlarmRptresultVo alarmReportVo = new AlarmRptresultVo();
            BeanUtil.copyProperties(labRptresult, alarmReportVo, "id");
            alarmRptresultVoList.add(alarmReportVo);
        }
        return alarmRptresultVoList;
    }

    /**
     * 获取危急值信息,危急值服务调用
     *
     * @param alarmReportDto
     * @return
     */
    @Override
    public List<AlarmReportVo> findAlarmInfo(AlarmReportDto alarmReportDto) {
        LambdaQueryWrapper<LabCriticalReport> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(LabCriticalReport::getDelFlag, 0);
        wrapper.and(i -> i.eq(LabCriticalReport::getIsReviewed, alarmReportDto.getIsReviewed()).or().isNull(LabCriticalReport::getIsReviewed));

        Timestamp startTime = Timestamp.valueOf(alarmReportDto.getBeginDate().substring(0, 10) + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(alarmReportDto.getEndDate().substring(0, 10) + " 23:59:59");

        wrapper.ge(LabCriticalReport::getReportTime, startTime).le(LabCriticalReport::getReportTime, endTime);

        List<LabCriticalReport> labCriticalReports = criticalReportMapper.selectList(wrapper);

        if (labCriticalReports.size() == 0) {
            return null;
        }

        List<AlarmReportVo> alarmReportVoList = new ArrayList<>();


        //迭代器遍历集合
        Iterator<LabCriticalReport> iterator = labCriticalReports.iterator();
        while (iterator.hasNext()) {
            LabCriticalReport criticalReport = iterator.next();
            AlarmReportVo alarmReportVo = new AlarmReportVo();

            Timestamp startTime1 = Timestamp.valueOf(criticalReport.getSampleda().toString().substring(0, 10) + " 00:00:00");
            Timestamp endTime1 = Timestamp.valueOf(criticalReport.getSampleda().toString().substring(0, 10) + " 23:59:59");

            LambdaQueryWrapper<LabMaininfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabMaininfo::getDelFlag, 0);

            // 添加一组条件，当RptGroup, SampleNo, Sampleda 都满足时
            lambdaQueryWrapper.and(w -> w.eq(LabMaininfo::getRptGroup, criticalReport.getRptGroup()).eq(LabMaininfo::getSampleNo, criticalReport.getSampleNo()).ge(LabMaininfo::getSampleda, startTime1).le(LabMaininfo::getSampleda, endTime1));

            List<LabMaininfo> labMaininfos = labMaininfoMapper.selectList(lambdaQueryWrapper);

            if (CollectionUtils.isEmpty(labMaininfos)) {
                return null;
            }
            LabMaininfo labMaininfo = labMaininfos.get(0);
            //判断病人类型
            if (alarmReportDto.getPatType() != null) {
                if (alarmReportDto.getPatType().size() > 0) {
                    boolean b = alarmReportDto.getPatType().contains(labMaininfo.getPatType());
                    if (!b) {
                        iterator.remove();
                        // labCriticalReports.remove(criticalReport);
                        continue;
                    }
                }
            }
            //判断科室
            if (alarmReportDto.getReqDept() != null) {
                if (alarmReportDto.getReqDept().size() > 0) {
                    boolean b = alarmReportDto.getReqDept().contains(labMaininfo.getReqDept());
                    if (!b) {
                        iterator.remove();
                        // labCriticalReports.remove(criticalReport);
                        continue;
                    }
                }
            }

            //判断报告单元
            if (alarmReportDto.getRptGroup() != null) {
                if (alarmReportDto.getRptGroup().size() > 0) {
                    if (!Objects.equals(alarmReportDto.getRptGroup(), labMaininfo.getRptGroup())) {
                        iterator.remove();
                        // labCriticalReports.remove(criticalReport);
                        continue;
                    }
                }
            }
            int isReviewed;
            if (criticalReport.getIsReviewed() == null || criticalReport.getIsReviewed() == 0) {
                isReviewed = 0;
            } else {
                isReviewed = 1;
            }

            alarmReportVo.setAlarmId(criticalReport.getId());
            alarmReportVo.setNoticeDate(criticalReport.getReportTime());
            alarmReportVo.setIsReceived(isReviewed);
            alarmReportVo.setReqDept(labMaininfo.getReqDept());
            alarmReportVo.setReqDoctor(labMaininfo.getReqDoctorNa());
            alarmReportVo.setReqNo(labMaininfo.getReqNo());
            alarmReportVo.setPatNo(labMaininfo.getPatNo());
            alarmReportVo.setPatNa(labMaininfo.getPatNa());
            alarmReportVo.setSex(labMaininfo.getSex());
            alarmReportVo.setTreatmentMeasure(criticalReport.getTreatmentMeasure());
            alarmReportVo.setBedNo(labMaininfo.getBedNo());
            alarmReportVo.setTestPurpose(labMaininfo.getTestPurpose());
            alarmReportVo.setSampleDa(labMaininfo.getSampleda());
            alarmReportVo.setReportGroup(labMaininfo.getRptGroup());
            alarmReportVo.setSampleNo(labMaininfo.getSampleNo());
            alarmReportVo.setTestDoctor(labMaininfo.getTestDoctorNa());
            alarmReportVo.setChkDoctor(labMaininfo.getChkDoctorNa());
            alarmReportVo.setChkTime(labMaininfo.getChkTime());
            alarmReportVo.setMainInfoId(labMaininfo.getId());

            String resultId = criticalReport.getResultId();
            LabRptresult labRptresult = labRptresultMapper.selectById(resultId);
            if (labRptresult == null) {
                return null;
            }

            alarmReportVo.setItemNo(labRptresult.getItemNo());
            alarmReportVo.setItemNa(labRptresult.getItemNa());
            alarmReportVo.setResultText(labRptresult.getTestResult());
            alarmReportVo.setResultRef(labRptresult.getRef());
            alarmReportVo.setReviewerDoctor(criticalReport.getReviewerDoctor());
            alarmReportVo.setReviewerTime(criticalReport.getReviewerTime());
            alarmReportVoList.add(alarmReportVo);
        }

        if (alarmReportVoList.size() == 0) {
            return null;
        }

        return alarmReportVoList;
    }

    @Override
    public Result getTreatedCriticalReport(getTreatedCriticalReportParam input) {
        if (StringUtils.isBlank(input.getRptGroup())) {
            return Result.fail("报告单元为空");
        }
        if (StringUtils.isBlank(input.getSampleNo())) {
            return Result.fail("标本号为空");
        }
        if (BeanUtil.isEmpty(input.getSampleda())) {
            return Result.fail("检验日期为空");
        }
        MPJLambdaWrapper<LabRptresult> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(LabRptresult::getDelFlag, 0);
        lambdaQueryWrapper.eq(LabRptresult::getIsAlarm, 1);
        lambdaQueryWrapper.eq(LabRptresult::getRptGroup, input.getRptGroup());
        lambdaQueryWrapper.eq(LabRptresult::getSampleNo, input.getSampleNo());
        Timestamp timestamp = new Timestamp(input.getSampleda().getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(timestamp);
        Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
        lambdaQueryWrapper.ge(LabRptresult::getSampleda, startTime);
        lambdaQueryWrapper.le(LabRptresult::getSampleda, endTime);
        List<LabRptresult> rptresList = labRptresultMapper.selectList(lambdaQueryWrapper);

        //List<LabCriticalReport>

        if (rptresList.isEmpty()) {
            return Result.succ(1, "无数据", null);
        }

        List<String> ids = rptresList.stream().map(LabRptresult::getId).collect(Collectors.toList());

        MPJLambdaWrapper<LabCriticalReport> query = new MPJLambdaWrapper<>();
        query.eq(LabCriticalReport::getDelFlag, 0);
        query.in(LabCriticalReport::getResultId, ids);
        query.and(p -> p.isNotNull(LabCriticalReport::getReviewerTime).isNotNull(LabCriticalReport::getReviewerDoctor).ne(LabCriticalReport::getReviewerDoctor, "").isNotNull(LabCriticalReport::getTreatmentMeasure).ne(LabCriticalReport::getTreatmentMeasure, ""));

        List<LabCriticalReport> dataList = criticalReportMapper.selectList(query);
        if (dataList.isEmpty()) {
            return Result.succ(1, "无数据", null);
        }

        return Result.succ(1, "成功", dataList);
    }
}
